package com.example.stresstest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.Future;

/**
 * @author zoRRo
 * @date 2020/11/7
 **/
@Slf4j
@Service
public class HttpService {
    
    @Async("taskExecutor")
    public Future<Long> doGet(String url) {
        long startTime = System.currentTimeMillis();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        long endTime = System.currentTimeMillis();
        log.info("响应码：" + responseEntity.getStatusCodeValue() + " 执行时长：" + (endTime - startTime) + "ms");
        return new AsyncResult<>(endTime - startTime);
    }
}
